import 'dart:io';

import 'package:permission_handler/permission_handler.dart';

class FMSystemAuthManager {

  /// 检查相机权限的状态
  Future<bool> checkCameraPermission() async {
    PermissionStatus status = await Permission.camera.status;
    return status.isGranted;
  }

  /// 打开相机权限的状态
  Future<void> requestCameraPermission() async {
    PermissionStatus status = await Permission.camera.status;

    if (status == PermissionStatus.granted) {return;}

    if (status == PermissionStatus.denied) {
      bool isPermanentlyDenied = await Permission.camera.isPermanentlyDenied;

      if (isPermanentlyDenied) {
        openAppSettings(); // 跳转到应用程序设置页面
      } else {
        PermissionStatus newStatus = await Permission.camera.request();

        if (newStatus == PermissionStatus.denied) {
          // 用户仍然拒绝了权限请求
          // 显示提示对话框或自定义消息
        }
      }
    }else {
      openAppSettings(); // 跳转到应用程序设置页面
    }
  }

  ///检查相册权限的状态
  Future<bool> checkGalleryPermission() async {
    Permission permission;
    if(Platform.isAndroid){
      permission = Permission.storage;
    }else {
      permission = Permission.photos;
    }
    PermissionStatus status = await permission.status;
    return status.isGranted;
  }
  /// 打开相册权限的状态
  Future<void> requestGalleryPermission() async {
    Permission permission;
    if(Platform.isAndroid){
      permission = Permission.storage;
    }else {
      permission = Permission.photos;
    }
    PermissionStatus status = await permission.status;

    if (status == PermissionStatus.granted) {return;}

    if (status == PermissionStatus.denied) {
      bool isPermanentlyDenied = await permission.isPermanentlyDenied;

      if (isPermanentlyDenied) {
        openAppSettings(); // 跳转到应用程序设置页面
      } else {
        PermissionStatus newStatus = await permission.request();

        if (newStatus == PermissionStatus.denied) {
          // 用户仍然拒绝了权限请求
          // 显示提示对话框或自定义消息
          openAppSettings();
        }
      }
    }else {
      openAppSettings(); // 跳转到应用程序设置页面
    }
  }

  /// 检查通知权限的状态
  Future<bool> checkNotificationPermission() async {
    PermissionStatus status = await Permission.notification.status;
    return status.isGranted;
  }

  /// 打开通知权限的状态
  Future<void> requestNotificationPermission() async {
    PermissionStatus status = await Permission.notification.status;

    if (status == PermissionStatus.granted) {return;}

    if (status == PermissionStatus.denied) {
      bool isPermanentlyDenied = await Permission.notification.isPermanentlyDenied;

      if (isPermanentlyDenied) {
        openAppSettings(); // 跳转到应用程序设置页面
      } else {
        PermissionStatus newStatus = await Permission.notification.request();

        if (newStatus == PermissionStatus.denied) {
          // 用户仍然拒绝了权限请求
          // 显示提示对话框或自定义消息
          if(Platform.isAndroid){
            openAppSettings();
          }
        }
      }
    }else {
      openAppSettings(); // 跳转到应用程序设置页面
    }
  }

  /// 检查日历权限的状态
  Future<bool> checkCalendarPermission() async {
    PermissionStatus status = await Permission.calendarFullAccess.status;
    return status.isGranted;
  }

  /// 打开日历权限的状态
  Future<void> requestCalendarPermission() async {
    PermissionStatus status = await Permission.calendarFullAccess.status;

    if (status == PermissionStatus.granted) {return;}

    if (status == PermissionStatus.denied) {
      bool isPermanentlyDenied = await Permission.calendarFullAccess.isPermanentlyDenied;

      if (isPermanentlyDenied) {
        openAppSettings(); // 跳转到应用程序设置页面
      } else {
        PermissionStatus newStatus = await Permission.calendarFullAccess.request();

        if (newStatus == PermissionStatus.denied) {
          // 用户仍然拒绝了权限请求
          // 显示提示对话框或自定义消息
          if(Platform.isAndroid){
            openAppSettings();
          }
        }
      }
    }else {
      openAppSettings(); // 跳转到应用程序设置页面
    }
  }

}